
from django.http import HttpResponse
import json
import sys
# from app.c import index as indexForecast
import app.c.index as Forecast
from app.c.mysql import mysqlDeploy
import os

#获取位置
def getPosition(request):

    # userId = request.GET.get('userId', 'abc')
    userId = 3
    data = getIdFromUserId(userId)

    position = getPositionById(data[0][0])

    print(position[0][0])
    # keyword = request.POST.get('keyword', 'abc')
    # return HttpResponse([state], content_type="application/json")

#取消指定用户的所有队列
def clearStateUserId(request):
    # userId = request.GET.get('userId', 'abc')
    userId = 1
    updateStateMysql(userId)
    print("清除成功，可以加入新队列")


#插入数据
def insertSearch(request):
    # userId = request.GET.get('userId', 'abc')
    userId = 3
    data = getCountFromUserId(userId)

    if data[0][0] != 0:
        print("有数据正在队列中 是否关闭之前的")
    else:
        insertIntoMysql(userId)

#杀死进程
def killProcess(request):
    ret_text_list = os.popen("ps | grep python")
    pid_list = []

    for line in ret_text_list:
        pid_list.append(line)

    cmd_list = pid_list[0].split()
    pid_num = cmd_list[1]

    os.system("kill -9 %s" % pid_num)



#——————————————————————————————————————————函数
def getPositionById(Id):
    mysqlS = mysqlDeploy()
    sql = "select count(1) from calQueue where id < %s and state = 0" % (Id)
    data = mysqlS.selectData(sql)
    return data

def getIdFromUserId(userId):
    mysqlS = mysqlDeploy()
    sql = "select id from calQueue where userId = %s order by id Desc limit 1" % (userId)
    data = mysqlS.selectData(sql)
    return data

def getCountFromUserId(userId):
    mysqlS = mysqlDeploy()
    sql = "select count(1) from calQueue where userId = %s and state in(0,1)" % (userId)
    data = mysqlS.selectData(sql)
    return data

def insertIntoMysql(userId):
    mysqlS = mysqlDeploy()
    sql = "insert into calQueue(userId) values(%s)" % (userId)
    data = mysqlS.insertInto(sql)
    return data

def updateStateMysql(userId):
    mysqlS = mysqlDeploy()
    sql = "update calQueue set state=1 where userId=(%s)"  % (userId)
    data = mysqlS.insertInto(sql)
    return data

def getState():
    pass

# clearStateUserId(1)
# insertSearch(1)
# getPosition(1)

# killProcess(1)



